package xian.woniu.service.impl;

import ch.qos.logback.core.pattern.ConverterUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xian.woniu.entity.Doctor;
import xian.woniu.entity.Orders;
import xian.woniu.entity.Patient;
import xian.woniu.entity.OrdersExample;
import xian.woniu.entity.OrdersExample;
import xian.woniu.mapper.OrdersMapper;
import xian.woniu.service.IDoctorService;
import xian.woniu.mapper.custom.CustomOrdersMapper;
import xian.woniu.service.IOrdersService;
import xian.woniu.service.IPatientService;
import xian.woniu.util.ResultVO;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author SongHang
 * @time 2022/06/29 16:11:45
 */
@Service
@Transactional
public class OrdersServiceImpl implements IOrdersService {
    private CustomOrdersMapper ordersMapper;
    //private CustomOrdersMapper customOrdersMapper;

    @Autowired
    private IPatientService patientService;
    @Autowired


    private IDoctorService doctorService;



    public OrdersServiceImpl(CustomOrdersMapper ordersMapper) {
        this.ordersMapper = ordersMapper;
    }

    @Override
    public void createOrder(Orders order) {
        ordersMapper.insertSelective(order);
    }

    @Override
    public void deleteOrder(Integer oid) {
        ordersMapper.deleteByPrimaryKey(oid);
    }

    @Override
    public void updateOrder(Orders order) {
        ordersMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public List<Orders> findAllOrders() {
        return ordersMapper.selectByExample(null);
    }

    @Override
    public Orders findOrderByOid(Integer oid) {
        return ordersMapper.selectByPrimaryKey(oid);
    }

    @Override
    public List<Orders> getAllSignPatient() {
        return ordersMapper.getAllSignPatient();
    }

    @Override
    public List<Orders> getAllPatientByDid(Integer did) {
        OrdersExample ordersExample=new OrdersExample();
        OrdersExample.Criteria criteria = ordersExample.createCriteria();
        criteria.andDidEqualTo(did);
        List<Orders> orders = ordersMapper.selectByExample(ordersExample);
        return orders;
    }

    @Override
    public List<Orders> getAllOrders() {
        List<Orders> ordersList = ordersMapper.getAllOrders();
        for(Orders order:ordersList){
            Integer did = order.getDid();
            Integer pid = order.getPid();
            ResultVO oned = doctorService.findOne(String.valueOf(did));
            ResultVO onep = patientService.selPatinfo1(String.valueOf(pid));
            Map docdata = (Map) oned.getData();
            Map patdata = (Map) onep.getData();
            Doctor doctor=new Doctor();
            Patient patient=new Patient();
            DateConverter converter=new DateConverter();
            converter.setPattern(new String("yyyy-MM-dd"));
            ConvertUtils.register(converter, Date.class);
            try {
                BeanUtils.populate(patient,patdata);
                BeanUtils.populate(doctor,docdata);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            order.setPatient(patient);
            order.setDoctor(doctor);
        }
        System.out.println("ordersList = " + ordersList);
        return ordersList;
    }

    @Override
    public List<Integer> findDoctorId(Integer pid) {

        return ordersMapper.findAllDid(pid);

    }

    @Override
    public List<Orders> findByDid(Integer did) {
        OrdersExample ordersExample=new OrdersExample();
        OrdersExample.Criteria criteria = ordersExample.createCriteria();
        criteria.andDidEqualTo(did);
        return ordersMapper.selectByExample(ordersExample);
    }


    @Override
    public  List<Orders> findOrderByPid(Integer pid) {
        List<Orders> ordersList = ordersMapper.selectByPid(pid);
        return ordersList;
    }
}
